package com.benyi.util;

/**
 * 用于生产地区代码：省市区镇村, 之间用 "-" 符号连接,长度分别为 2,2,2,2,3 个支付(0到Z) 最长为  15 个字符
 * @author admin
 *
 */
public class AreaCode {
	private static final String chars="0123456789abcdefghijklmnopqrstuvwxyz";
	private static final char split_char='-';
	
	/**
	 * 根据当前的代码获取下一个编码
	 * @param cur
	 * @return
	 */
	public static String nextCode(String cur){
		if(cur==null){
			return "10";
		}
		if(cur.length()==2){ //省
			return nextProvince(cur);
		}
		if(cur.length()==4){//市
			return nextCity(cur);
		}
		if(cur.length()==6){//区
			return nextRegion(cur);
		}
		if(cur.length()==8){//镇
			return nextTown(cur);
		}
		if(cur.length()==11){//村
			return nextVillage(cur);
		}
		return null;
	}
	
	public static String nextVillage(String code){
		if(code==null || code.length()==0){
			return null;
		}
		code=code.replaceAll(String.valueOf(split_char),"");
		
		if(code.length()==8){
			String v=code+"100";
			v=format(v);
			return v;
		}
		code=code.toLowerCase();
		if(code.length()>11){
			code=code.substring(0,11);
		}
		if(code.length()<11){
			return null;
		}
		
		int p1=chars.indexOf(code.charAt(8));
		int p2=chars.indexOf(code.charAt(9));
		int p3=chars.indexOf(code.charAt(10));
		
		if(p1<0 || p2<0 || p3<0){
			return null;
		}
		if(p3<chars.length()-1){
			p3++;
		}else{
			if(p2<chars.length()-1){
				p2++;
				p3=0;
			}else{
				p1++;
				p2=0;
				p3=0;
				if(p1>=chars.length()){
					return null;
				}
			}
		}
		String v = code.substring(0,8) + String.valueOf(chars.charAt(p1)) + 
				String.valueOf(chars.charAt(p2)) + String.valueOf(chars.charAt(p3));
		v=format(v);
		return v; 
	}
	
	private static String format(String v){
		if(v==null){
			return v;
		}
		v=v.toUpperCase();
		if(v.length()==2){
			return v;
		}else
		if(v.length()==4){
			v=v.substring(0,2) + String.valueOf(split_char) + v.substring(2,4);
		}else
		if(v.length()==6){
			v=v.substring(0,2) + String.valueOf(split_char) + v.substring(2,4) +
			  String.valueOf(split_char) + v.substring(4,6);
		}else
		if(v.length()==8){
			v=v.substring(0,2) + String.valueOf(split_char) + v.substring(2,4) +
					  String.valueOf(split_char) + v.substring(4,6)+
					  String.valueOf(split_char) + v.substring(6,8);
		}else
		if(v.length()==11){
			v=v.substring(0,2) + String.valueOf(split_char) + v.substring(2,4) +
					  String.valueOf(split_char) + v.substring(4,6)+
					  String.valueOf(split_char) + v.substring(6,8)+
					  String.valueOf(split_char) + v.substring(8,11);
		}
		return v;
	}
	
	
	public static String nextTown(String code){
		if(code==null || code.length()==0){
			return null;
		}
		code=code.replaceAll(String.valueOf(split_char),"");
		
		if(code.length()==6){
			String v=code+"10";
			v=format(v);
			return v;
		}
		code=code.toLowerCase();
		
		if(code.length()>8){
			code=code.substring(0,8);
		}
		if(code.length()<8){
			return null;
		}
				
		int p1=chars.indexOf(code.charAt(6));
		int p2=chars.indexOf(code.charAt(7));
		if(p1<0 || p2<0){
			return null;
		}
		if(p2<chars.length()-1){
			p2++;
		}else{
			p2=0;
			p1++;
			if(p1>=chars.length()){
				return null;
			}
		}
		String v = code.substring(0,6) + String.valueOf(chars.charAt(p1)) + String.valueOf(chars.charAt(p2));
		v=format(v);
		return v; 
	}
	
	
	public static String nextRegion(String code){
		if(code==null || code.length()==0){
			return null;
		}
		code=code.replaceAll(String.valueOf(split_char),"");
		
		if(code.length()==4){
			String v=code+"10";
			v=format(v);
			return v;
		}
		
		code=code.toLowerCase();		
		
		if(code.length()>6){
			code=code.substring(0,6);
		}
		if(code.length()<6){
			return null;
		}
		
		int p1=chars.indexOf(code.charAt(4));
		int p2=chars.indexOf(code.charAt(5));
		if(p1<0 || p2<0){
			return null;
		}
		if(p2<chars.length()-1){
			p2++;
		}else{
			p2=0;
			p1++;
			if(p1>=chars.length()){
				return null;
			}
		}
		String v = code.substring(0,4) + String.valueOf(chars.charAt(p1)) + String.valueOf(chars.charAt(p2));
		v=format(v);
		return v; 
	}
	
	
	public static String nextCity(String code){
		if(code==null || code.length()==0){
			return null;
		}
		code=code.replaceAll(String.valueOf(split_char),"");
		
		if(code.length()==2){
			String v=code+"10";
			v=format(v);
			return v;
		}
		code=code.toLowerCase(); 
		
		if(code.length()>4){
			code=code.substring(0,4);
		}
		if(code.length()<4){
			return null;
		}
				
		int p1=chars.indexOf(code.charAt(2));
		int p2=chars.indexOf(code.charAt(3));
		if(p1<0 || p2<0){
			return null;
		}
		if(p2<chars.length()-1){
			p2++;
		}else{
			p2=0;
			p1++;
			if(p1>=chars.length()){
				return null;
			}
		}
		String v = code.substring(0,2) + String.valueOf(chars.charAt(p1)) + String.valueOf(chars.charAt(p2));
		v=format(v);
		return v; 
	}
	
	public static String nextProvince(String code){
		if(code==null || code.length()==0){
			return "10";
		}
		code=code.toLowerCase();
		code=code.replaceAll(String.valueOf(split_char),"");
		
		if(code.length()>2){
			code=code.substring(0,2);
		}
		if(code.length()<2){
			return null;
		}
		
		int p1=chars.indexOf(code.charAt(0));
		int p2=chars.indexOf(code.charAt(1));
		if(p1<0 || p2<0){
			return null;
		}
		if(p2<chars.length()-1){
			p2++;
		}else{
			p2=0;
			p1++;
			if(p1>=chars.length()){
				return null;
			}
		}
		String v = String.valueOf(chars.charAt(p1))+String.valueOf(chars.charAt(p2));
		v=format(v);
		return v; 
	}
	
	public static void main(String []args){
		String v="1010ZI10100";
		for(int i=0;i<100000;i++){
			//v=nextProvince(v);
			//v=nextCity(v);
			//v=nextRegion(v);
			//v=nextTown(v);
			v=nextVillage(v);
			System.out.println(i+":"+v);
			if(v==null){
				break;
			}
		}
	}
}
